package com.example.zg.zg_312_ordering_new.model;

import com.example.zg.zg_312_ordering_new.beans.User;
import com.example.zg.zg_312_ordering_new.common.Constants;
import com.example.zg.zg_312_ordering_new.listener.FoodOrderListener;
import com.example.zg.zg_312_ordering_new.listener.FoodOrderiface;
import com.example.zg.zg_312_ordering_new.services.FoodOrderService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodOrderModel implements FoodOrderiface {
    private  Retrofit retrofit;
    public FoodOrderModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void insertOrder(int user_id, int food_id, int num, double sum, String suggesttime, String address, final FoodOrderListener foodOrderListener) {
        FoodOrderService foodOrderService=retrofit.create(FoodOrderService.class);
        Call<User> call=foodOrderService.insertOrder(user_id, food_id, num, sum, suggesttime, address);
        call.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    if (response.isSuccessful()&&response!=null)
                        foodOrderListener.onResponse(response.body());
                    else
                        foodOrderListener.onFail("onresponse fail");
                }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                foodOrderListener.onFail(t.toString());
            }
        });
    }
}
